﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Owin.Hosting;
using nwrole.toolbox.sync.client;
using Topshelf;
using nwrole.Module.lib;

namespace nwrole.toolbox.sync.ConfigModule
{
    class Program
    {
        static void Main(string[] args)
        {
            new ModuleStarter(new SyncConfigApp(), args);

            /*var exitCode = HostFactory.Run(host =>
            {
                host.Service<SyncConfigApp>(service =>
                {
                    service.ConstructUsing(() => new SyncConfigApp());
                    service.WhenStarted(a => a.Start());
                    service.WhenStopped(a => a.Stop());
                });

                host.SetDescription("Manages nwrole modules");
                host.SetDisplayName("nwrole SuperModule service");
                host.SetServiceName("nwrole.SuperModule.service");
                host.RunAsNetworkService();
            });
            return (int)exitCode;*/
        }
    }

    class SyncConfigApp : IModule
    {
        private static IDisposable _webApplication;

        // Source Service Connection Info

        [ModuleConfig(Name = "SyncSrcServiceConnType")]
        public string SyncSrcServiceConnType { get; set; }

        [ModuleConfig(Name = "SyncSourceService/ServiceBus/ServiceNamespace")]
        public string SyncSrcServiceSBServiceNamespace { get; set; }

        [ModuleConfig(Name = "SyncSourceService/ServiceBus/ServicePath")]
        public string SyncSrcServiceSBServicePath { get; set; }

        [ModuleConfig(Name = "SyncSourceService/ServiceBus/IssuerName")]
        public string SyncSrcServiceSBIssuerName { get; set; }

        [ModuleConfig(Name = "SyncSourceService/ServiceBus/IssuerSecret")]
        public string SyncSrcServiceSBIssuerSecret { get; set; }

        [ModuleConfig(Name = "SyncSourceService/WebService/BaseAddress")]
        public string SyncSrcServiceWSBaseAddress { get; set; }

        [ModuleConfig(Name = "SyncSourceService/WebService/Address")]
        public string SyncSrcServiceWSAddress { get; set; }

        // Destination Service Connection Info

        [ModuleConfig(Name = "SyncDestinationService/ConnType")]
        public string SyncDestServiceConnType { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/ServiceNamespace")]
        public string SyncDestServiceSBServiceNamespace { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/ServicePath")]
        public string SyncDestServiceSBServicePath { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/IssuerName")]
        public string SyncDestServiceSBIssuerName { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/IssuerSecret")]
        public string SyncDestServiceSBIssuerSecret { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/WebService/BaseAddress")]
        public string SyncDestServiceWSBaseAddress { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/WebService/Address")]
        public string SyncDestServiceWSAddress { get; set; }

        private AutoResetEvent _waitEvent = new AutoResetEvent(false);

        public void Start(IModuleDebug debug)
        {
            try
            {
                SiteSettings.SyncSourceConnInfo = SyncSourceConnInfo;
                SiteSettings.SyncDestinationConnInfo = SyncDestinationConnInfo;

                _webApplication = WebApp.Start<WebPipeline>("http://localhost:" + 5010);
                //SuperModule.Start();

                Trace.TraceInformation("ConfigModule running");

                _waitEvent.WaitOne();

                _webApplication.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }

        public void Stop()
        {
            Trace.TraceInformation("Stopping ConfigModule");

            _waitEvent.Set();
            
            //SuperModule.Stop();
        }

        private ServiceConnectionInfo SyncSourceConnInfo
        {
            get
            {
                ServiceConnectionInfo connInfo = new ServiceConnectionInfo();

                ServiceConnectionInfo.ServiceConnectionType type;
                ServiceConnectionInfo.ServiceConnectionType.TryParse(SyncSrcServiceConnType, true, out type);
                connInfo.Type = type;

                connInfo.ServiceBusServiceNamespace = SyncSrcServiceSBServiceNamespace;
                connInfo.ServiceBusServicePath = SyncSrcServiceSBServicePath;
                connInfo.ServiceBusIssuerName = SyncSrcServiceSBIssuerName;
                connInfo.ServiceBusIssuerSecret = SyncSrcServiceSBIssuerSecret;

                connInfo.WebServiceBaseAddress = SyncSrcServiceWSBaseAddress;
                connInfo.WebServiceAddress = SyncSrcServiceWSAddress;

                return connInfo;
            }
        }

        public ServiceConnectionInfo SyncDestinationConnInfo
        {
            get
            {
                ServiceConnectionInfo connInfo = new ServiceConnectionInfo();

                ServiceConnectionInfo.ServiceConnectionType type;
                ServiceConnectionInfo.ServiceConnectionType.TryParse(SyncDestServiceConnType, true, out type);
                connInfo.Type = type;

                connInfo.ServiceBusServiceNamespace = SyncDestServiceSBServiceNamespace;
                connInfo.ServiceBusServicePath = SyncDestServiceSBServicePath;
                connInfo.ServiceBusIssuerName = SyncDestServiceSBIssuerName;
                connInfo.ServiceBusIssuerSecret = SyncDestServiceSBIssuerSecret;

                connInfo.WebServiceBaseAddress = SyncDestServiceWSBaseAddress;
                connInfo.WebServiceAddress = SyncDestServiceWSAddress;

                return connInfo;
            }
        }
    }
}
